package com.web_bloks.storage.client;

import java.util.Enumeration;
import java.util.Vector;

import com.web_bloks.system.client.Logger;
import com.web_bloks.system.client.Time;

/**
 * 
 * @author Jean-Lou Dupont
 * 
 * TODO implement clean-up
 */
public class RecentList 
	extends GearsLocalStorageTemplate 
	implements IRecentList {

	final static String DEFAULT_TABLE = "RecentList";
	
	String tableName = null;
	
	/**
	 * Ctor
	 */
	public RecentList() {
		super();
	}
	
	public RecentList(String tableName) {
		super();
		this.tableName = tableName;
	}
	/*****************************************
	 * base class
	 *****************************************/
	public String getSchema() {
		return "(entry TEXT, meta TEXT, created INT)";
	}

	public String getStorageName() {
		return "LocalStorage";
	}

	/**
	 * Provides a default value
	 */
	public String getTableName() {
		
		if (null!=tableName)
			return tableName;
		
		return DEFAULT_TABLE;
	}

	/*****************************************
	 * API
	 *****************************************/
	public boolean insertEntry(String entry, String meta) {
		
		//Verify that the entry being inserted
		//isn't already also at the top...
		if ( !verifyTop( entry) )
			return true;
		
		int now = Time.getTime();
		
		String[] values = new String[3];
		
		values[0] = entry;
		values[1] = meta;
		values[2] = Integer.toString( now );

		boolean result = true;
		
		try {
			put("entry, meta, created", values );
		} catch (LocalStorageException e) {
			result = false;
		}
		
		return result;
	}
	/**
	 * Pulls the last 'count' entries from the db
	 * 
	 * @param count
	 * @return Vector<RecentListEntry> OR null
	 */
	public Vector<RecentListEntry> getLatest(int count) {
		
		Vector<String[]> resultSet = null;
		
		try {
			resultSet = get("entry, meta, created", 3, "ORDER BY created DESC LIMIT "+count, null);
		} catch (LocalStorageException e) {
			Logger.logError( getClass()+".getLatest: exception, msg="+e.getMessage() );
			return null;
		}
		
		if (null==resultSet) return null;
	
		Vector<RecentListEntry> result = new Vector<RecentListEntry>();
		
		Enumeration<String[]> liste = resultSet.elements();
		
		while(liste.hasMoreElements()) {
			
			String[] e = liste.nextElement();
			RecentListEntry entry = new RecentListEntry( e[0], e[1], Long.parseLong( e[2] ) );
			result.add( entry );
		}
		
		return result;
	}
	
	/**
	 * 
	 * @param entry
	 * @return boolean response
	 */
	protected boolean verifyTop(String entry) {
		
		Vector<String[]> resultSet = null;
		
		try {
			resultSet = get("entry, meta, created", 3, "ORDER BY created DESC LIMIT 1", null);
		} catch (LocalStorageException e) {
			Logger.logError( getClass()+".getLatest: exception, msg="+e.getMessage() );
			return true;
		}
		
		if (null==resultSet) return true; //OK to insert
		
		String[] topElement = resultSet.elementAt( 0 );
		
		if ( null == topElement ) return true;
		
		return topElement[0] != entry;
		
	}//
	
}//